#include <QStyleOption>
#include <QPainter>
#include <QFileDialog>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "Image.h"
#include "MeanFilter.h"
#include "MedianFilter.h"
#include "luminance.h"
#include "contrastratio.h"
#include "greychange.h"
#include "medianfilterwindow.h"
#include "meanfilterwindow.h"
#include "sgmtation.h"
#include "woodcut.h"
#include "temperature.h"
#include "edge.h"
#include "sharpen.h"
#include "colorlayers.h"
#include "information.h"

Image photo;
QString tmp0;
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_1_clicked()
{
    tmp0=ui->textEdit->toPlainText();
    QByteArray tmp1=tmp0.toUtf8();
    char *str=tmp1.data();
    if(photo.ReadBMP(str)){
        ui->label->setPixmap(QPixmap::fromImage(photo).scaled(ui->label->size(),Qt::KeepAspectRatio));
    }
    else{
        Attention *attentionWindow = new Attention;
        attentionWindow->setWindowModality(Qt::ApplicationModal);
        attentionWindow->show();
    }
}

void MainWindow::on_pushButton_2_clicked()
{
    MeanFilterWindow *mea=new MeanFilterWindow;
    mea->setWindowModality(Qt::ApplicationModal);
    mea->show();
    connect(mea,SIGNAL(sendData(int)),this,SLOT(receiveData4(int)));
}

void MainWindow::on_pushButton_3_clicked()
{
    MedianFilterWindow *med=new MedianFilterWindow;
    med->setWindowModality(Qt::ApplicationModal);
    med->show();
    connect(med,SIGNAL(sendData(int)),this,SLOT(receiveData5(int)));
}

void MainWindow::receiveData4(int data)
{
    if(data<0)
        data=0;
    if(data>0&&data%2==0)
        data=data/2*2-1;
    MeanFilter f(data);
    photo=f.Filtering(photo);
    ui->label->setPixmap(QPixmap::fromImage(photo).scaled(ui->label->size(),Qt::KeepAspectRatio));
}

void MainWindow::receiveData5(int data)
{
    if(data<0)
        data=0;
    if(data>0&&data%2==0)
        data=data/2*2-1;
    MedianFilter f(data);
    photo=f.Filtering(photo);
    ui->label->setPixmap(QPixmap::fromImage(photo).scaled(ui->label->size(),Qt::KeepAspectRatio));
}

void MainWindow::on_pushButton_5_clicked()
{
    photo.Flip_LR();
    ui->label->setPixmap(QPixmap::fromImage(photo).scaled(ui->label->size(),Qt::KeepAspectRatio));
}


void MainWindow::on_pushButton_4_clicked()
{
    photo.Flip_UD();
    ui->label->setPixmap(QPixmap::fromImage(photo).scaled(ui->label->size(),Qt::KeepAspectRatio));
}


void MainWindow::on_pushButton_6_clicked()
{
    photo.Rotate();
    ui->label->setPixmap(QPixmap::fromImage(photo).scaled(ui->label->size(),Qt::KeepAspectRatio));
}


void MainWindow::on_pushButton_7_clicked()
{
    Luminance *lum=new Luminance;
    lum->setWindowModality(Qt::ApplicationModal);
    lum->show();
    connect(lum,SIGNAL(sendData(int)),this,SLOT(receiveData(int)));
}


void MainWindow::on_pushButton_8_clicked()
{
    ContrastRatio *cr=new ContrastRatio;
    cr->setWindowModality(Qt::ApplicationModal);
    cr->show();
    connect(cr,SIGNAL(sendData(float)),this,SLOT(receiveData2(float)));
}

void MainWindow::receiveData(int data)
{
    photo.BrightLine(1,data);
    ui->label->setPixmap(QPixmap::fromImage(photo).scaled(ui->label->size(),Qt::KeepAspectRatio));
}

void MainWindow::receiveData2(float data)
{
    photo.BrightLine(data);
    ui->label->setPixmap(QPixmap::fromImage(photo).scaled(ui->label->size(),Qt::KeepAspectRatio));
}

void MainWindow::on_pushButton_clicked()
{
    QString fileName = QFileDialog::getOpenFileName(this,QStringLiteral("选择图片"),"C:",QStringLiteral("图像文件(*bmp)"));
    ui->textEdit->setText(fileName);
}

void MainWindow::paintEvent(QPaintEvent *)
{
    QStyleOption opt;
    opt.initFrom(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}

void MainWindow::on_pushButton_9_clicked()
{
    photo.Rotate2();
    ui->label->setPixmap(QPixmap::fromImage(photo).scaled(ui->label->size(),Qt::KeepAspectRatio));
}

void MainWindow::on_pushButton_10_clicked()
{
    GreyChange *gc=new GreyChange;
    gc->setWindowModality(Qt::ApplicationModal);
    gc->show();
    connect(gc,SIGNAL(sendData(float)),this,SLOT(receiveData3(float)));
}

void MainWindow::on_pushButton_11_clicked()
{
    photo.Inversion();
    ui->label->setPixmap(QPixmap::fromImage(photo).scaled(ui->label->size(),Qt::KeepAspectRatio));
}

void MainWindow::receiveData3(float data)
{
    photo.GrayChange(data);
    ui->label->setPixmap(QPixmap::fromImage(photo).scaled(ui->label->size(),Qt::KeepAspectRatio));
}

void MainWindow::on_pushButton_12_clicked()
{
    Sgmtation *sg=new Sgmtation;
    sg->setWindowModality(Qt::ApplicationModal);
    sg->show();
    connect(sg,SIGNAL(sendData(int,int,int,int)),this,SLOT(receiveData7(int,int,int,int)));
}

void MainWindow::on_pushButton_13_clicked()
{
    Woodcut *wc=new Woodcut;
    wc->setWindowModality(Qt::ApplicationModal);
    wc->show();
    connect(wc,SIGNAL(sendData(int)),this,SLOT(receiveData6(int)));
}

void MainWindow::receiveData6(int data)
{
    photo.Woodcut(data);
    ui->label->setPixmap(QPixmap::fromImage(photo).scaled(ui->label->size(),Qt::KeepAspectRatio));
}

void MainWindow::receiveData7(int delta,int r,int g,int b)
{
    photo.Sgmtation(delta,r,g,b);
    ui->label->setPixmap(QPixmap::fromImage(photo).scaled(ui->label->size(),Qt::KeepAspectRatio));
}

void MainWindow::on_pushButton_15_clicked()
{
    Temperature *t=new Temperature;
    t->setWindowModality(Qt::ApplicationModal);
    t->show();
    connect(t,SIGNAL(sendData(int)),this,SLOT(receiveData8(int)));
}

void MainWindow::receiveData8(int data)
{
    photo.Colortemperature(data);
    ui->label->setPixmap(QPixmap::fromImage(photo).scaled(ui->label->size(),Qt::KeepAspectRatio));
}

void MainWindow::on_pushButton_14_clicked()
{
    Edge *e=new Edge;
    e->setWindowModality(Qt::ApplicationModal);
    e->show();
    connect(e,SIGNAL(sendData(int,int,int,int)),this,SLOT(receiveData9(int,int,int,int)));
}

void MainWindow::receiveData9(int data,int r,int g,int b)
{
    photo.FindEdge(data,r,g,b);
    ui->label->setPixmap(QPixmap::fromImage(photo).scaled(ui->label->size(),Qt::KeepAspectRatio));
}


void MainWindow::on_pushButton_16_clicked()
{
    Sharpen *sp=new Sharpen;
    sp->setWindowModality(Qt::ApplicationModal);
    sp->show();
    connect(sp,SIGNAL(sendData(float)),this,SLOT(receiveData10(float)));
}

void MainWindow::receiveData10(float data)
{
    photo.Sharpen(data);
    ui->label->setPixmap(QPixmap::fromImage(photo).scaled(ui->label->size(),Qt::KeepAspectRatio));
}

void MainWindow::receiveData11(int data)
{
    photo.CoulorLayers(data);
    ui->label->setPixmap(QPixmap::fromImage(photo).scaled(ui->label->size(),Qt::KeepAspectRatio));
}

void MainWindow::on_pushButton_17_clicked()
{
    ColorLayers *cl=new ColorLayers;
    cl->setWindowModality(Qt::ApplicationModal);
    cl->show();
    connect(cl,SIGNAL(sendData(int)),this,SLOT(receiveData11(int)));
}

void MainWindow::on_pushButton_18_clicked()
{
    QByteArray tmp1=tmp0.toUtf8();
    char *str=tmp1.data();
    photo.WriteBMP(str);
}

void MainWindow::on_pushButton_19_clicked()
{
    QString fileName = QFileDialog::getSaveFileName(this,QStringLiteral("选择存储位置"),"./",QStringLiteral("图像文件(*bmp)"));
    if (QFileInfo(fileName).suffix().isEmpty())
        {
            fileName += ".bmp";
        }
    QByteArray tmp1=fileName.toUtf8();
    char *str=tmp1.data();
    photo.WriteBMP(str);
}

void MainWindow::on_pushButton_20_clicked()
{
    Information *ifm=new Information;
    ifm->setWindowModality(Qt::ApplicationModal);
    ifm->show();
}
